home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / FROMUTS / UNIXLIB37B / test / c / pipetest < prev    next >
Text File  |  1991-09-27  |  663b  |  54 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include "sys/unix.h"
  5.  
  6. static void copyio(void)
  7. {
  8. char buf[512];
  9. register int n;
  10.  
  11. while ((n = read(0,buf,512)) > 0) write(1,buf,n);
  12. }
  13.  
  14. int main()
  15. {
  16. int p[2];
  17.  
  18. if (pipe(p) < 0) { perror("pipe()"); exit(1); }
  19.  
  20. switch (vfork())
  21.   {
  22.   case -1:
  23.     perror("vfork()");
  24.     exit(1);
  25.     break;
  26.   case 0:
  27.     close(p[0]);
  28.     dup2(p[1],1);
  29.     close(p[1]);
  30.     copyio();
  31.     _exit(0);
  32.     break;
  33.   default:
  34.     break;
  35.   }
  36.  
  37. switch (vfork())
  38.   {
  39.   case -1:
  40.     perror("vfork()");
  41.     exit(1);
  42.     break;
  43.   case 0:
  44.     close(p[1]);
  45.     dup2(p[0],0);
  46.     close(p[0]);
  47.     copyio();
  48.     _exit(0);
  49.     break;
  50.   default:
  51.     break;
  52.   }
  53. }
  54.